﻿using MijnMultiOSRSSApp.Common.Commands;
using MijnMultiOSRSSApp.Common.Models;
using MijnMultiOSRSSApp.Common.Services;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MijnMultiOSRSSApp.Common.ViewModels
{
    public class MainViewModel : BaseViewModel
    {
        private List<RSSItem> _items1;
        public List<RSSItem> Items1
        {
            get { return _items1; }
            set
            {
                if (value == _items1) return;
                _items1 = value;
                RaisePropertyChanged("Items1");
            }
        }
        private List<RSSItem> _items2;
        public List<RSSItem> Items2
        {
            get { return _items2; }
            set
            {
                if (value == _items2) return;
                _items2 = value;
                RaisePropertyChanged("Items2");
            }
        }
        private List<RSSItem> _items3;
        public List<RSSItem> Items3
        {
            get { return _items3; }
            set
            {
                if (value == _items3) return;
                _items3 = value;
                RaisePropertyChanged("Items3");
            }
        }
        private List<RSSItem> _items4;
        public List<RSSItem> Items4
        {
            get { return _items4; }
            set
            {
                if (value == _items4) return;
                _items4 = value;
                RaisePropertyChanged("Items4");
            }
        }
        private List<RSSItem> _items5;
        public List<RSSItem> Items5
        {
            get { return _items5; }
            set
            {
                if (value == _items5) return;
                _items5 = value;
                RaisePropertyChanged("Items5");
            }
        }
        private List<RSSItem> _items6;
        public List<RSSItem> Items6
        {
            get { return _items6; }
            set
            {
                if (value == _items6) return;
                _items6 = value;
                RaisePropertyChanged("Items6");
            }
        }
        private List<RSSItem> _items7;
        public List<RSSItem> Items7
        {
            get { return _items7; }
            set
            {
                if (value == _items7) return;
                _items7 = value;
                RaisePropertyChanged("Items7");
            }
        }
        private List<RSSItem> _items8;
        public List<RSSItem> Items8
        {
            get { return _items8; }
            set
            {
                if (value == _items8) return;
                _items8 = value;
                RaisePropertyChanged("Items8");
            }
        }
        private List<RSSItem> _items9;
        public List<RSSItem> Items9
        {
            get { return _items9; }
            set
            {
                if (value == _items9) return;
                _items9 = value;
                RaisePropertyChanged("Items9");
            }
        }
        private List<RSSItem> _items10;
        public List<RSSItem> Items10
        {
            get { return _items10; }
            set
            {
                if (value == _items10) return;
                _items10 = value;
                RaisePropertyChanged("Items10");
            }
        }
        private RSSItem _selectedItem;
        public RSSItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if (value == _selectedItem) return;
                _selectedItem = value;
                RaisePropertyChanged("SelectedItem");
            }
        }

        private bool _hasErrorOccurred;
        public bool HasErrorOccurred
        {
            get { return _hasErrorOccurred; }
            set
            {
                if (value == _hasErrorOccurred) return;
                _hasErrorOccurred = value;
                RaisePropertyChanged("HasErrorOccurred");
            }
        }

        public AboutCommand About { get; set; }
        public SettingsCommand Settings { get; set; }
        public BrowseUrlCommand BrowseUrl { get; set; }
        public RefreshCommand Refresh { get; set; }
        public ShareCommand Share { get; set; }

        public MainViewModel()
        {
            About = new AboutCommand();
            Settings = new SettingsCommand();
            BrowseUrl = new BrowseUrlCommand();
            Refresh = new RefreshCommand();
            Share = new ShareCommand();
        }

        public async void LoadData()
        {
            IsDataLoaded = false;

            HasErrorOccurred = false;

            try
            {
                var items = await RssService.GetItems(Resources.Configuration.FeedUrl5);
                Items5 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl1);
                if (items != null)
                    Items1 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl2);
                if (items != null)
                    Items2 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl3);
                if (items != null)
                    Items3 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl4);
                if (items != null)
                    Items4 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl6);
                if (items != null)
                    Items6 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl7);
                if (items != null)
                    Items7 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl8);
                if (items != null)
                    Items8 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl9);
                if (items != null)
                    Items9 = items.ToList();
                items = await RssService.GetItems(Resources.Configuration.FeedUrl10);
                if (items != null)
                    Items10 = items.ToList();
            }
            catch (Exception e)
            {
                HasErrorOccurred = true;
            }

            IsDataLoaded = true;
        }
    }
}